package com.ftg.learn.chapter04;

import java.util.Arrays;
import java.util.Objects;

/**
 * 线性表的顺序存储
 * @author kangjx
 * @createTime 2022/6/30
 */
public class MyArrayList<T> implements MyList<T>{

    /**
     * 私有类型数据
     */
    private T[] arr;

    private int index = 0;

    private int defaultCapacity = 10;

    private double expan = 0.5;

    public MyArrayList() {

    }

    /**
     * 构造方法
     *
     * @param initCapacity 初始长度
     */
    public MyArrayList(int initCapacity) {
        if (initCapacity <= 0) {
            throw new ArrayIndexOutOfBoundsException("长度应该大于0");
        }
        arr = (T[]) (new Object[defaultCapacity]);
    }

    public MyArrayList(int initCapacity, double expan) {
        if (initCapacity <= 0) {
            throw new ArrayIndexOutOfBoundsException("长度应该大于0");
        }
        if (expan < 0 || expan > 1) {
            throw new RuntimeException("扩容因子大于0");
        }
        arr = (T[]) (new Object[defaultCapacity]);
        this.expan = expan;
    }

    @Override
    public boolean add(T val) {
        //chushi hua
        this.initArr();

        //kong rong
        this.expanCapacity();
        //tianjia
        this.arr[this.index++] = val;
        return true;
    }

    /**
     * puvc
     */
    private void initArr() {
        if (Objects.isNull(arr)) {
            this.arr = (T[]) (new Object[defaultCapacity]);
        }
    }

    /**
     *
     */
    private void expanCapacity() {
        if (this.arr.length - this.index == 0) {
            int newLen = this.arr.length + (int) (this.arr.length * this.expan);
            this.arr = Arrays.copyOf(this.arr, newLen);
        }
    }

    @Override
    public T get(int index) {
        this.checkIndex(index);
        if(index > this.index){
            return null;
        }
        return this.arr[index];
    }

    @Override
    public boolean update(int index, T newVal) {
        this.checkIndex(index);
        grentThenMax(index,this.index);
        this.arr[index] = newVal;
        return true;
    }

    private void checkIndex(int index){
        if(index < 0){
            throw new ArrayIndexOutOfBoundsException("长度应该大于0");
        }
    }

    @Override
    public boolean delete(int t) {

        this.checkIndex(t);

        //后面元素向移动几位
        System.arraycopy(this.arr,t+1,this.arr,t,this.index - t - 1);
        this.index--;

        return true;
    }

//    @Override
//    public boolean delete(T val) {
//        T[] as = (T[])new Object[this.index];
//        int a = 0;
//        for(int i = 0 ,len = this.arr.length;i < len;i++){
//            if(this.arr[i] == val){
//                this.index--;
//            }else{
//                as[a++] = this.arr[i];
//            }
//        }
//        this.arr = as;
//        return false;
//    }


    @Override
    public boolean delete(T val) {
        int a = 0;
        for(int i = 0 ,len = this.index;i < len;i++){
            if(this.arr[i] != val){
               this.arr[a++] = this.arr[i];
            }
        }
        this.index = a;
        return false;
    }

    @Override
    public int size() {
        return this.index;
    }

}
